var express = require('express');
const { get } = require('mongoose');
var moment = require("moment")
var router = express.Router();
var { bannerModel } =require("../model/bannermodel")
var { goodModel } =require("../model/goods");
const {classifyModel} = require('../model/classify');
/* GET home page. */
router.get('/banner', async function(req, res, next) {
 let data = await bannerModel.find()
//  console.log(data);
 res.send({
    code:200,
    data
 })
});
router.post("/good/:id",async(req,res)=>{
   let id = req.params.id
   let data = [];
   console.log(id);
  if (id == 1) {
    data = await goodModel.find().populate("classid");
  } else {
    data = await goodModel.find({classid:id}).populate("classid");
  }
  // console.log(data,'data');
  res.send({
    code: 200,
    data,
  });
 })
 router.get("/search",async(req,res)=>{
    let classid = req.query.classid
    let name = req.query.name
   let obj = {};
  //  console.log(classid,name);
   if(classid && name){
      obj.classid=classid
      obj.name={$regex:name}
   }
  else if (classid) {
   obj.classid={$in:[classid]}
   let classData = await classifyModel.find({_id:classid})
   console.log(classData,'222');
   if (classData[0].childrenid.length>0){
   obj.classid={$in:[classid,...classData[0].childrenid]}
   }
   console.log(obj.classid,555);
  }
  else if(name){
     obj.name={$regex:name}
  }else{
    obj ={}
  }

  // console.log(obj,'data');
  let data = await goodModel.find(obj).populate("classid");
//  console.log(data,'111');
  res.send({
    code: 200,
    data,
  });
 })

module.exports = router;